package com.ruoyi.project.modules.errorCodeCategory.service.impl;

import java.util.List;
import java.util.ArrayList;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.errorCodeCategory.mapper.ErrorCodeCategoryMapper;
import com.ruoyi.project.modules.errorCodeCategory.domain.ErrorCodeCategory;
import com.ruoyi.project.modules.errorCodeCategory.service.IErrorCodeCategoryService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 错误码分类Service业务层处理
 *
 * @author smallrain
 * @date 2022-04-04
 */
@Service
public class ErrorCodeCategoryServiceImpl implements IErrorCodeCategoryService {
    @Resource
    private ErrorCodeCategoryMapper errorCodeCategoryMapper;

    /**
     * 查询错误码分类
     *
     * @param errorCodeCategoryId 错误码分类主键
     * @return 错误码分类
     */
    @Override
    public ErrorCodeCategory selectErrorCodeCategoryByErrorCodeCategoryId(Long errorCodeCategoryId) {
        return errorCodeCategoryMapper.selectErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategoryId);
    }

    /**
     * 查询错误码分类列表
     *
     * @param errorCodeCategory 错误码分类
     * @return 错误码分类
     */
    @Override
    public List<ErrorCodeCategory> selectErrorCodeCategoryList(ErrorCodeCategory errorCodeCategory) {
        return errorCodeCategoryMapper.selectErrorCodeCategoryList(errorCodeCategory);
    }

    /**
     * 新增错误码分类
     *
     * @param errorCodeCategory 错误码分类
     * @return 结果
     */
    @Override
    public synchronized int insertErrorCodeCategory(ErrorCodeCategory errorCodeCategory) {
        String errorCodeCategoryCode = errorCodeCategory.getErrorCodeCategoryCode();
        if (errorCodeCategory.getParenterrorCodeCategoryId() != null) {
            ErrorCodeCategory errorCodeCategory1 = this.errorCodeCategoryMapper.selectErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategory.getParenterrorCodeCategoryId());
            errorCodeCategoryCode = errorCodeCategory1.getErrorCodeCategoryCode() + errorCodeCategoryCode;
        } else {
            errorCodeCategory.setParenterrorCodeCategoryId(0l);
        }
        ErrorCodeCategory errorCodeCategory1 = new ErrorCodeCategory();
        errorCodeCategory1.setErrorCodeCategoryCode(errorCodeCategoryCode);
        List<ErrorCodeCategory> errorCodeCategories = this.errorCodeCategoryMapper.selectErrorCodeCategoryList(errorCodeCategory1);
        if (CollectionUtil.isNotEmpty(errorCodeCategories)) {
            throw new BusinessException("错误码分类编码重复");
        }
        errorCodeCategory.setErrorCodeCategoryCode(errorCodeCategoryCode);
        return errorCodeCategoryMapper.insertErrorCodeCategory(errorCodeCategory);
    }

    /**
     * 修改错误码分类
     *
     * @param errorCodeCategory 错误码分类
     * @return 结果
     */
    @Override
    public int updateErrorCodeCategory(ErrorCodeCategory errorCodeCategory) {
        return errorCodeCategoryMapper.updateErrorCodeCategory(errorCodeCategory);
    }

    /**
     * 批量删除错误码分类
     *
     * @param errorCodeCategoryIds 需要删除的错误码分类主键
     * @return 结果
     */
    @Override
    public int deleteErrorCodeCategoryByErrorCodeCategoryIds(String errorCodeCategoryIds) {
        return errorCodeCategoryMapper.deleteErrorCodeCategoryByErrorCodeCategoryIds(Convert.toStrArray(errorCodeCategoryIds));
    }

    /**
     * 删除错误码分类信息
     *
     * @param errorCodeCategoryId 错误码分类主键
     * @return 结果
     */
    @Override
    public int deleteErrorCodeCategoryByErrorCodeCategoryId(Long errorCodeCategoryId) {
        return errorCodeCategoryMapper.deleteErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategoryId);
    }

    /**
     * 查询错误码分类树列表
     *
     * @return 所有错误码分类信息
     */
    @Override
    public List<Ztree> selectErrorCodeCategoryTree() {
        List<ErrorCodeCategory> errorCodeCategoryList = errorCodeCategoryMapper.selectErrorCodeCategoryList(new ErrorCodeCategory());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ErrorCodeCategory errorCodeCategory : errorCodeCategoryList) {
            Ztree ztree = new Ztree();
            ztree.setId(errorCodeCategory.getErrorCodeCategoryId());
            ztree.setpId(errorCodeCategory.getParenterrorCodeCategoryId());
            ztree.setName(errorCodeCategory.getErrorCodeCategoryName());
            ztree.setTitle(errorCodeCategory.getErrorCodeCategoryName());
            ztree.setCode(errorCodeCategory.getErrorCodeCategoryCode());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
